import Vue from "vue";
import App from "./App.vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import VueRouter from "vue-router";
import config from "../common/config.js"
import * as utils from '../common/utils.js';
import api from './request/api';

// import qiniu from'qiniu' // 需要加载qiniu模块的

import {
	Message
} from "element-ui"

import './assets/iconfont.js'
import './assets/iconfont.css'

Vue.prototype.$config = config
Vue.prototype.$api = api //全局请求方法
Vue.prototype.$utils = utils //全局公用函数
Vue.prototype.$globalVariant = {
	fileChanged: false
}

Vue.config.productionTip = false;

Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(config)
Vue.use(api)
Vue.use(utils)

const router = new VueRouter({
	mode: "history",
	routes: [{
			path: "/",
			redirect: "/login"
		},
		// 用户注册
		{
			path: "/register",
			component: () => import("@/components/register.vue")
		},
		{
			path: "/login",
			component: () => import("@/components/index.vue")
		},
		//主页
		{
			path: "/home",
			component: () => import("@/components/home.vue"),
			meta: {
				title: '主页',
				type: 'login' // 是否需要判断是否登录,这里是需要判断
			},
		},
		//好友
		{
			path: "/friend",
			component: () => import("@/components/friend.vue"),
			meta: {
				title: '主页',
				type: 'login' // 是否需要判断是否登录,这里是需要判断
			},
		},
		//随手记
		{
			path: "/note",
			component: () => import("@/components/markdown.vue"),
			meta: {
				title: '随手记',
				type: 'login' // 是否需要判断是否登录,这里是需要判断
			}
		},
		//番茄钟
		{
			path: "/tree",
			component: () => import("@/components/tree.vue"),
			meta: {
				title: '番茄钟',
				type: 'login' // 是否需要判断是否登录,这里是需要判断
			}
		},
		//日历
		{
			path: "/time",
			component: () => import("@/components/time.vue"),
			meta: {
				title: '日历',
				type: 'login' // 是否需要判断是否登录,这里是需要判断
			},
		},


	]
});
var vm = new Vue({
	render: h => h(App),
	router
}).$mount("#app");

router.afterEach((to) => {
	if (to.meta.type === 'login') {
		if (!window.localStorage.getItem('login') || !window.localStorage.getItem('uid')) {
			//已经登录过了
			Message({
				showClose: true,
				message: '您还没有登录喔，请先登录',
				type: 'warning'
			});
			vm.$router.replace({
				path: '/login', // 到登录页重新获取token
			})
		}
	}
})

router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title
	}
	// 判断该路由是否需要登录权限
	if (to.meta.type === 'login') {
		if (window.localStorage.getItem('login') && window.localStorage.getItem('uid')) {
			//已经登录过了
			next()
		} else {
			Message({
				showClose: true,
				message: '您还没有登录喔，请先登录',
				type: 'warning'
			});
			next('/login')
		}
	} else {
		next() // 确保一定要有next()被调用
	}
})
export {
	vm
}
